// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: gc_trace_visitor.cpp
// Description: GCTraceVisitor Class
//      Author: Ziming Li
//     Created: 2021-09-10
// -------------------------------------------------
// Copyright (C) 2021 MingYang Software Technology.
// -------------------------------------------------

#include "./gc_trace_visitor.h"

#include "blinkit/blink/renderer/wtf/MainThread.h"
#include "blinkit/gc/gc_object_set.h"

namespace BlinKit {

GCObject* GCTraceVisitor::PopSlot(GCSession &session)
{
    GCRefPtrBase *slot = zed::pop(m_slotQueue);

    GCObject *o = slot->m_object;
    ASSERT(nullptr != o);

    GCSession::Slots &slots = session.MemberObjects[o];
    if (!zed::key_exists(slots, slot))
    {
        slots.emplace(slot);
        if (1 == slots.size())
            return o; // This object hasn't been traced yet.
    }

    return nullptr;
}

void GCTraceVisitor::TraceImpl(GCRefPtrBase &ptr)
{
    ASSERT(isMainThread());
    m_slotQueue.emplace(&ptr);
}

void GCTraceVisitor::TraceObjectSet(GCObjectSetCallback &callback)
{
    callback.EnumObjects([](GCObject &o) {
        ASSERT(false); // BKTODO:
    });
}

} // namespace BlinKit
